package org.pjlib.net;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.blankj.utilcode.util.StringUtils;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
import org.pjlib.Event;
import org.pjlib.common.AppKey;

/**
 * Created by ws on 2019/02/21.
 */
public class RequestCall {
    public static int SUCCESS ;
    private HttpHeaders headers;
    private HttpParams params;
    private String url;
    private int requestCode;
    private Object data;
    private JSONObject json;
    private String msg;
    private int status;
    private int total;
    private DataType dataType= DataType.JSON;
    protected static String MSG_KEY ;
    protected static String STATUS_KEY ;
    protected static String DATA_KEY ;
    protected static String TOTAL;
    public static int NoLoginStatus;
    public static void init(String msgKey,String statusKey,String dataKey,String total){
        SPUtils.getInstance().put("msgKey",msgKey);
        SPUtils.getInstance().put("statusKey",statusKey);
        SPUtils.getInstance().put("dataKey",dataKey);
        SPUtils.getInstance().put("total",total);

        MSG_KEY=msgKey;
        STATUS_KEY=statusKey;
        DATA_KEY=dataKey;
        TOTAL=total;
    }
    public RequestCall() {
    }
    public boolean isSuccess(){
        return status==SUCCESS;
    }
    public RequestCall(String url, int requestCode) {
        this.url = url;
        this.requestCode = requestCode;
    }

    public RequestCall(String url, int requestCode,HttpParams params) {
        this.params = params;
        this.url = url;
        this.requestCode = requestCode;
    }


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public HttpHeaders getHeaders() {
        return headers;
    }

    public void setHeaders(HttpHeaders headers) {
        this.headers = headers;
    }

    public HttpParams getParams() {
        return params;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public DataType getDataType() {
        return dataType;
    }

    public void setDataType(DataType dataType) {
        this.dataType = dataType;
    }

    public void setParams(HttpParams params) {
        this.params = params;
    }

    public int getRequestCode() {
        return requestCode;
    }

    public void setRequestCode(int requestCode) {
        this.requestCode = requestCode;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public JSONObject getJson() {
        return json;
    }

    public final void setJson(JSONObject json) {
        this.json = json;
        perse(json);
    }

    private void perse(JSONObject json) {
        if (json == null) {
            return;
        }
        if(StringUtils.isEmpty(MSG_KEY)){
            MSG_KEY=SPUtils.getInstance().getString("msgKey","msgs");
        }
        if(StringUtils.isEmpty(STATUS_KEY)){
            STATUS_KEY=SPUtils.getInstance().getString("statusKey","status");
        }
        if(StringUtils.isEmpty(DATA_KEY)){
            DATA_KEY=SPUtils.getInstance().getString("dataKey","datas");
        }
        if(StringUtils.isEmpty(TOTAL)){
            TOTAL=SPUtils.getInstance().getString("total","");
        }
        msg = JSONUtils.getJStr(json, MSG_KEY);
        status = JSONUtils.getJNum(json, STATUS_KEY);
        if(!StringUtils.isEmpty(TOTAL)){
            total=JSONUtils.getJNum(json,TOTAL);
        }
        if(NoLoginStatus!=0 && status==NoLoginStatus){
            AppKey.putAppKey("");
            EventBus.getDefault().post(new Event.Login());
        }
        switch (dataType) {
            case JSON:
                data = JSONUtils.getJJson(json, DATA_KEY);
                break;
            case JSONARR:
                data = JSONUtils.getJJsonArr(json, DATA_KEY);
                break;
            default:
                data = JSONUtils.getJStr(json, DATA_KEY);
                break;
        }
    }

    public enum DataType {
        JSON, String, JSONARR
    }
}
